package com.mixsmart.core.service.impl;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mixsmart.core.IMixSmart;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.core.dao.IExecuteDao;
import com.mixsmart.core.service.IResExecuteService;
import com.mixsmart.core.xml.sqlmap.SqlMapUtils;
import com.mixsmart.core.xml.sqlmap.bean.SqlUnit;
import com.mixsmart.utils.StringUtils;

/**
 * 根据资源名称执行SQL或HQL语句（实现类）
 * @version 1.0
 * @since JDK1.6以上
 * @author lmq
 * <br />
 * 2016年7月23日
 */
@Service
public class ResExecuteService implements IResExecuteService {

	@Autowired
	private IExecuteDao executeDao;

	@Override
	public MixResponse<String> execute(String resName, HashMap<String, Object> params) {
		MixResponse<String> mixResp = new MixResponse<String>();
		mixResp.setMsg(IMixSmart.OP_FAIL_MSG);
		if(StringUtils.isNotEmpty(resName)) {
			SqlUnit sqlUnit = SqlMapUtils.getSql(resName);
			if(null != sqlUnit) {
				if(sqlUnit.isFilter()) {
					executeDao.executeByFilter(sqlUnit.getSql(), params);
				} else {
					executeDao.execute(sqlUnit.getSql(), params);
				}
				mixResp.setResult(IMixSmart.OP_SUCCESS);
				mixResp.setMsg("执行成功");
			}
		}
		return mixResp;
	}

	@Override
	public MixResponse<String> executes(String resName, HashMap<String, Object> params) {
		MixResponse<String> mixResp = new MixResponse<String>();
		mixResp.setMsg(IMixSmart.OP_FAIL_MSG);
		if(StringUtils.isNotEmpty(resName)) {
			SqlUnit sqlUnit = SqlMapUtils.getSql(resName);
			if(null != sqlUnit) {
				String[] sqls = sqlUnit.getSql().split(IMixSmart.MULTI_STATEMENT_SPLIT);
				if(sqlUnit.isFilter()) {
					executeDao.executesByFilter(sqls, params);
				} else {
					executeDao.executes(sqls, params);
				}
				mixResp.setResult(IMixSmart.OP_SUCCESS);
				mixResp.setMsg("执行成功");
			}
		}
		return mixResp;
	}

	@Override
	public MixResponse<String> executeHQL(String resName, HashMap<String, Object> params) {
		MixResponse<String> mixResp = new MixResponse<String>();
		mixResp.setMsg(IMixSmart.OP_FAIL_MSG);
		if(StringUtils.isNotEmpty(resName)) {
			SqlUnit sqlUnit = SqlMapUtils.getSql(resName);
			if(null != sqlUnit) {
				if(sqlUnit.isFilter()) {
					executeDao.executeHQLByFilter(sqlUnit.getSql(), params);
				} else {
					executeDao.executeHQL(sqlUnit.getSql(), params);
				}
				mixResp.setResult(IMixSmart.OP_SUCCESS);
				mixResp.setMsg("执行成功");
			}
		}
		return mixResp;
	}

	@Override
	public MixResponse<String> executesHQL(String resName, HashMap<String, Object> params) {
		MixResponse<String> mixResp = new MixResponse<String>();
		mixResp.setMsg(IMixSmart.OP_FAIL_MSG);
		if(StringUtils.isNotEmpty(resName)) {
			SqlUnit sqlUnit = SqlMapUtils.getSql(resName);
			if(null != sqlUnit) {
				String[] sqls = sqlUnit.getSql().split(IMixSmart.MULTI_STATEMENT_SPLIT);
				if(sqlUnit.isFilter()) {
					executeDao.executesHQLByFilter(sqls, params);
				} else {
					executeDao.executesHQL(sqls, params);
				}
				mixResp.setResult(IMixSmart.OP_SUCCESS);
				mixResp.setMsg("执行成功");
			}
		}
		return mixResp;
	}
	
}
